package tech.aistar.controller;

import org.springframework.web.bind.annotation.*;
import tech.aistar.pojo.User;

/**
 * 本类用来演示: 遵守restful软甲架构风格
 *
 * @author: success
 * @date: 2021/5/10 2:15 下午
 */
@RestController
@RequestMapping("/product")
public class RestFulController {

    /**
     * 查询所有的产品...
     * @return
     */
    @GetMapping
    public String hello(){
        return "product - ok";
    }

    /**
     * http://localhost:8888/mvc/product/100
     * 根据id进行查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")// @GetMapping("/getId")
    public String hello2(@PathVariable("id") Integer id){
        return "id:"+id;
    }

    /**
     * http://localhost:8888/mvc/product/100/admin
     * @param id
     * @param name
     * @return
     */
    @GetMapping("/{id}/{name}")// @GetMapping("/getId")
    public String hello3(@PathVariable("id") Integer id,@PathVariable("name") String name){
        return "id:"+id+",name:"+name;
    }

    /**
     * $.ajax({
     *     type:'PUT'
     *
     * })
     *
     * 修改对象
     * @return
     */
    @PutMapping
    public User hello4(User user){
        user.setName("admin");
        return user;
    }

    /**
     * 增加一个对象
     * @param user
     * @return
     */
    @PostMapping
    public User hello5(User user){
        user.setName("tom");
        return user;
    }

//    @PostMapping
//    public User hello6(@RequestBody User user){
//        user.setName("jack");
//        return user;
//    }
}
